package _mst;

/**
 * 面试题 08.10. 颜色填充
 */
public class T0810 {
    private int[][] image;
    private int oldColor;
    private int newColor;

    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        this.image = image;
        this.oldColor = image[sr][sc];
        this.newColor = newColor;
        if (oldColor != newColor) dfs(sr, sc);
        return image;
    }

    private void dfs(int i, int j) {
        if (i < 0 || j < 0 || i >= image.length || j >= image[0].length || image[i][j] != oldColor) return;
        image[i][j] = newColor;
        dfs(i - 1, j);
        dfs(i + 1, j);
        dfs(i, j - 1);
        dfs(i, j + 1);
    }
}